// GuideDialog.ts
import { _decorator, Component, Label, Sprite, director, resources, SpriteFrame ,Node} from 'cc';
import { GuideEvent } from './GuideEvent';
const { ccclass, property } = _decorator;

@ccclass('GuideDialog')
export class GuideDialog extends Component {
    @property(Label)
    private dialogLabel: Label = null!;
    @property(Sprite)
    private arrowSprite: Sprite = null!;

    onLoad() {
        // 监听事件
        director.on(GuideEvent.SHOW_DIALOG, this.show, this);
        director.on(GuideEvent.HIDE_DIALOG, this.hide, this);

    }

    onDestroy() {
        // 取消监听
        director.off(GuideEvent.SHOW_DIALOG, this.show, this);
        director.off(GuideEvent.HIDE_DIALOG, this.hide, this);
    }

    private show(targetNode:Node,step: any) {
        this.dialogLabel.string = step.dialog;
        const worldPos = targetNode.getWorldPosition();
        this.dialogLabel.node.setWorldPosition(worldPos.x,worldPos.y-200,worldPos.z)
        this.arrowSprite.node.setWorldPosition(worldPos.x,worldPos.y-100,worldPos.z)
        this.loadArrow(step.arrow);
    }

    private hide() {
        this.dialogLabel.string = "";
        this.arrowSprite.spriteFrame = null;
    }

    private loadArrow(arrowDir: string) {
        resources.load(`guide/textures/CRS_HAND/spriteFrame`, SpriteFrame, (err, frame) => {
            if (err) {
                console.error(`箭头资源加载失败: ${err}`);
                return;
            }
            this.arrowSprite.spriteFrame = frame;
        });
    }
}